<template>
    <div>
        <el-form ref="ruleForm" :model="ruleForm"  label-width="80px">
            {{ ruleForm.ImgID }}
            <el-form-item label="商品图片" prop="Images">
                <el-upload class="avatar-uploader" :action="baseapi + '/Files'" :show-file-list="false"
                    :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                    <img v-if="ruleForm.Images" :src="baseimg + ruleForm.Images" class="avatar">
                    <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                </el-upload>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import basepath from '../../../plugins/base.js'
export default {
    name: 'userInfo',
    data() {
        return {
            baseimg: '',
            baseapi: '',
            ruleForm: {
                UserID: 0,
                UserName: "张得帅",
                Sex: "女",
                Birthday: new Date("2008-08-08"),
                ImgID: 3,
                Images: '',
                RoleID: 1,
                Phone: "13145678912",
                LoginName: "test1",
                LoginPwd: "123",
                Status: true
            },
        }
    },
    methods: {
        //上传成功
        handleAvatarSuccess(res, file) {
            console.log('res: ', res);

            if (res.code == 1) {
                this.$message.success('图片上传成功!');
                this.ruleForm.Images = res.data.Path;
                this.ruleForm.ImgID = res.data.ImgID;
            } else {
                this.$message.error(res.msg);
            }
        },
        //上传之前
        beforeAvatarUpload(file) {
            const isJPG = file.type === 'image/jpeg';
            const isPNG = file.type === 'image/png';

            const isLtMB = file.size / 1024 / 1024 < 10;

            if (!isJPG && !isPNG) {
                this.$message.error('上传头像图片只能是 JPG 和 PNG 格式!');
            }
            if (!isLtMB) {
                this.$message.error('上传头像图片大小不能超过 10MB!');
            }
            return (isJPG || isPNG) && isLtMB;
        },
    },
    created () {
        this.baseimg = basepath.imgUrl;
        this.baseapi = basepath.apiUrl;
    }
}
</script>

<style scoped>
.avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
}

.avatar-uploader .el-upload:hover {
    border-color: #409eff;
}

.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
}

.avatar {
    width: 178px;
    height: 178px;
    display: block;
}
</style>